package com.augurit.gzcgwsp.state.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.state.service.IYwSummaryStateService;
import com.augurit.gzcgwsp.state.util.RequestUtil;
import com.augurit.gzcgwsp.ywcmn.entity.BaseDistrict;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("ywSummaryStateAtcion")
@ParentPackage("struts-default")
@Namespace("/state")
@Scope("prototype")
@Action(value="ywsummary-state",results={
	 	@Result(name = "success", location = "/gzcgwsp/state/listXkState.jsp"),
		@Result(name = "toDb", location = "/gzcgwsp/state/listDb.jsp"),
		@Result(name = "toYjynys", location = "/gzcgwsp/state/listYjYnys.jsp"),
	 	@Result(name = "tohuanwei", location = "/gzcgwsp/state/listHwXkState.jsp"),
	 	@Result(name = "resultState", location = "/gzcgwsp/state/rqstate/listResultState.jsp"),
	 	@Result(name = "resultInfo", location = "/gzcgwsp/state/rqstate/listStateData.jsp")
	}
)
/**
 * 行政许可审批查询Action,此类的实现方式是从后台功能定义的时候先传一个yw的参数然后根据参数查询出每个处室的业务编码。
 */
public class YwSummaryStateAtcion extends ActionSupport implements ModelDriven {
	@Autowired
	private IYwSummaryStateService ywSummaryStateService;
	private Page page ;

	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	
	/**
	 * 全委待办任务查询
	 * @return
	 */
	public String toDb(){
		return "toDb";
	}
	
	/**
	 * 余泥运输查询
	 * @return
	 */
	public String toYjByynys(){
		return "toYjynys";
	}
	
	
	
	/**
	 * 获取区
	 * @return
	 */
	public String getArea(){
		List<BaseDistrict> list=this.ywSummaryStateService.getArea();
		 List<BasicCombo> combos = new ArrayList<BasicCombo>();
    	 if(list!=null && list.size()>0){
    		 for (int i=0;i<list.size();i++) {
    			BaseDistrict area=list.get(i);
 				BasicCombo combo = new BasicCombo();
 				combo.setLabel(area.getDistrictName());
 				combo.setValue(area.getDistrictName());
 				combos.add(combo);
 		 }
    	 }
		
		 ExtBaseAction.extRenderListJson(combos, true);
		return null;
	}
	
	
	/**
	 * 分页统计查询
	 * @return
	 */
    public String toList(){
    	//请求的查询数据
    Map map = RequestUtil.getMapByRequest(getRequest());
	String ywtype=(String) map.get("ywtype");//处室
	String ywname=this.getYwName(ywtype);
    String tempcodes=this.getTempcode(ywname);
    map.put("tempcodes", tempcodes);
    if(page==null){
  		  page = new Page(15);
  	 }
    ExtUtils.initPageFromExtGridParam(getRequest(), page);
  	page=this.ywSummaryStateService.search(page,map);
  	
  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
  	
  	return null;
  }
    
	/**
	 * 分页统计待办任务
	 * @return
	 */
    public String toDbList(){
    	//请求的查询数据
    Map map = RequestUtil.getMapByRequest(getRequest());
	String ywtype=(String) map.get("ywtype");//处室
	String ywname=this.getYwName(ywtype);
    String tempcodes=this.getTempcode(ywname);
    map.put("tempcodes", tempcodes);
    if(page==null){
  		  page = new Page(15);
  	 }
    ExtUtils.initPageFromExtGridParam(getRequest(), page);
  	page=this.ywSummaryStateService.searchDbData(page,map);
  	
  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
  	
  	return null;
  }
    
	/**
	 * 分页统计余泥任务
	 * @return
	 */
    public String toYjListByYnys(){
    	//请求的查询数据
    Map map = RequestUtil.getMapByRequest(getRequest());
    if(page==null){
  		  page = new Page(15);
  	 }
    ExtUtils.initPageFromExtGridParam(getRequest(), page);
    String templateCodes="'jzljczz_shhz','jzljczz_ysbb','jzljczz_bghz','jzljczz_nsxz','jzljczz_xzfz'";
    map.put("tempcodes", templateCodes);
  	page=this.ywSummaryStateService.searchYjData(page,map);
  	
  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
  	
  	return null;
  }
    
    

    /**
     * 到查询首页
     * @return
     */
    public String queryList(){
    	return Constant.QUERYlIST;
    }
    
	@Override
	public String execute() throws Exception {
		  Map map = RequestUtil.getMapByRequest(getRequest());
		  String ywtype=(String) map.get("yw");
		  getRequest().setAttribute("ywtype", ywtype);
		  if("huanwei".equals(ywtype)){
			  return "tohuanwei";
		  }else{
			  return SUCCESS;
		  }
		
	}
    
	/**
	 * 获取登录用户的呈批业务,用于查询待办已办的查询
	 * @return
	 */
	public String getCpyws(){
		String ywtype=this.getRequest().getParameter("ywtype");
		String ywname=this.getYwName(ywtype);
		 List list=this.ywSummaryStateService.getCpyws(ywname);
    	 List<BasicCombo> combos = new ArrayList<BasicCombo>();
    	 if(list!=null && list.size()>0){
    		 for (int i=0;i<list.size();i++) {
    			 Map map=(Map) list.get(i);
    			 String code=(String) map.get("template_code");
    			 String name=(String) map.get("template_name");
 				BasicCombo combo = new BasicCombo();
 				combo.setLabel(name);
 				combo.setValue(code);
 				combos.add(combo);
 		 }
    	 }
		
		 ExtBaseAction.extRenderListJson(combos, true);
		return null;
	}
	
	/**
	 * 获取业务名称
	 * @param ywtype
	 * @return
	 */
	private String getYwName(String ywtype){
		String name="";
		if("ranqi".equals(ywtype)){
			name="燃气";
		}else if("shirong".equals(ywtype)){
			name="市容";
		}else if("hwgg".equals(ywtype)){
			name="景观";
		}else if("sheshi".equals(ywtype)){
			name="设施";
		}else if("huanwei".equals(ywtype)){
			name="环卫";
		}else if("yuni".equals(ywtype)){
			name="余泥";
		}else if("quanwei".equals(ywtype)){
			name="余泥,环卫,设施,景观,市容,燃气";
		}else{
			LoginUserForm userForm=(LoginUserForm)getSession().getAttribute(WebContextUtils.SES_LOGIN_USER);
			OmOrgForm org=userForm.getOrg();
			String orgName=org.getOrgName();
			if(StringUtils.isNotBlank(orgName)){
				if(orgName.indexOf("市容")>=0){
					name="市容";
				}else if(orgName.indexOf("景观")>=0){
					name="景观";
				}else if(orgName.indexOf("设施")>=0){
					name="设施"; 
				}else if(orgName.indexOf("环卫")>=0){
					name="环卫";
				}else if(orgName.indexOf("燃气")>=0){
					name="燃气";
				}else {
					//余泥处的情况
					OmOrgForm prentOrg=userForm.getParentOrg();
					String parentName=prentOrg.getOrgName();
					if(parentName!=null && parentName.indexOf("余泥")>=0){
						name="余泥";
					}
					
				}
			}
			
		}
		return name;
		
	}
	
	/**
	 * 取得HttpSession的简化方法.
	 */
	public static HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	
	/**
	 * 获取查询需要的业务编码
	 */
	public String getTempcode(String ywname) {
		String codes="";
		List list=this.ywSummaryStateService.getCpyws(ywname);
		for(int i=0;i<list.size();i++){
			Map map=(Map) list.get(i);
			codes+="'"+(String)map.get("template_code")+"'"+",";
		}
		if(codes.length()>0){
			codes=codes.substring(0,codes.length()-1);
		}
		return codes;
	}
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	private static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	private static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	public Object getModel() {
		return null;
	}
	/**
	 * 直接输出文本.
	 * @see #render(String, String, String...)
	 */
	public static void renderText(final String text, final String... headers) {
		Struts2Utils.renderText(text, headers);
	}
}
